package com.ctibook.booksystem.service.bookcatalog.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import com.cti.ftpservice.ftp.DateHelper;
import com.cti.ftpservice.utils.IsbnConvertUtils;
import com.cti.ftpservice.vo.PageViewVO;
import com.ctibook.booksystem.dao.bookcatalog.BookInfoCategoryDao;
import com.ctibook.booksystem.dao.bookcatalog.BookInfoDao;
import com.ctibook.booksystem.dao.bookcatalog.BookInfoFullTextSearchDao;
import com.ctibook.booksystem.dao.bookcatalog.BookInfoPublisherDao;
import com.ctibook.booksystem.model.bookcatalog.BookInfo;
import com.ctibook.booksystem.model.bookcatalog.BookInfoCategory;
import com.ctibook.booksystem.model.bookcatalog.BookInfoExtral;
import com.ctibook.booksystem.model.bookcatalog.BookInfoPublisher;
import com.ctibook.booksystem.service.bookcatalog.BookInfoService;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoCategoryVO;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoCategoryVos;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoDetailsVO;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoExtralVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoPropertyLoadVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoPublisherVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoSearchNormalVO;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoStatisticsTJVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoUpdateVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoVO;


/**
 * @author 鲁永刚 E-mail: luyg@ctibook.com
 * @version 创建时间：Sep 12, 2014 10:17:30 AM 类说明
 */
@Service("bookInfoService")
public class BookInfoServiceImpl implements BookInfoService {

	private static Log log = LogFactory.getLog(BookInfoServiceImpl.class);
	private static DecimalFormat decimalFormat = new DecimalFormat(".00");

	@Resource
	private BookInfoDao bookInfoDao;
	
	
	@Resource
	private BookInfoFullTextSearchDao bookInfoFullTextSearchDao;
	
	@Resource
	private BookInfoCategoryDao bookInfoCategoryDao;
	
	@Resource
	private BookInfoPublisherDao bookInfoPublisherDao;
	/**
	 * 默认缩略图
	 */
	private static final String NO_IMAGE_THUMBS = "noimage_thumbs.jpg";
	private static final String TEMPLATE_PATH = "template/excel/booklist_template.xls";



	@Override
	public void addOrUpdateBookInfoSync(BookInfoDetailsVO vo,boolean isCheckIsbn) {
		BookInfo bookInfo = convertBookInfoDetailsVoToBookInfoNotReplacePropWhenNotHasValue(vo,isCheckIsbn);
		bookInfoDao.addOrUpdateBookInfo(bookInfo);
		if(bookInfo.getPublisherFullName()!=null)
			addOrUpdatePublisher(bookInfo.getPublisherFullName(),null);
		if(bookInfo.getCategory1()!=null)
			addOrUpdateCategory1AndCategory(bookInfo.getCategory1(), bookInfo.getCategory2());
	}


	
	/**
	 * 将bookinfoDetailsVo转换为bookinfo，当前没值时使用原来的值
	 * 
	 * @param vo
	 * @return
	 */
	private BookInfo convertBookInfoDetailsVoToBookInfoNotReplacePropWhenNotHasValue(BookInfoDetailsVO vo,boolean isCheckIsbn) {
		if (StringUtils.isBlank(vo.getIsbn())) {
			throw new RuntimeException("ISBN号不能为空");
		}
		String isbn = vo.getIsbn();
		if (isCheckIsbn && IsbnConvertUtils.validate(isbn)==false) {
			throw new RuntimeException("ISBN号不正确");
		}
		if (StringUtils.isBlank(vo.getTitle())) {
			throw new RuntimeException("题名不能为空");
		}
		
		//TODO: 出版社
		
        if(isCheckIsbn){
    		isbn = IsbnConvertUtils.convertTo13place(isbn);	
        }
		// 根据isbn号查找该书目
		BookInfo bookInfo = bookInfoDao.getBookInfoByIsbnAndStatus(isbn,null);
		if (bookInfo == null) 
			bookInfo = new BookInfo();
		BeanUtils.copyProperties(vo, bookInfo,"id","bookInfoExtral");
	
	    //设置时间
		
		if(vo.getPubYear()!=null){
			if(vo.getPubMonth()==null)
				vo.setPubMonth(0);
			if(vo.getPubDay()==null)
				vo.setPubDay(1);
			Date pubDate=DateHelper.getDate(vo.getPubYear(), vo.getPubMonth(), vo.getPubDay());
			bookInfo.setPubDate(pubDate);
			bookInfo.setPubYear(vo.getPubYear());
		}
		bookInfo.setIsbn(isbn);
		bookInfo.setLastUpdateDate(new Date());
		bookInfo.setPublisherFullName(vo.getPublisherFullName());
		bookInfo.setPublisherAlisName(vo.getPublisherAilsName());
		bookInfo.setCreateUserName(vo.getCreateUserName());
		
		if(vo.getBookInfoExtralVo()!=null){
			boolean saveOrNot=chargeBookInfoExtralVoNullOrNotNull(vo.getBookInfoExtralVo());
			if(saveOrNot){
				BookInfoExtral bookInfoExtral=bookInfoDao.getBookInfoExtralByISBN(isbn);
				if(bookInfoExtral==null&&vo.getBookInfoExtralVo().getId()!=null)
					bookInfoExtral=bookInfoDao.getBookInfoExtralById(vo.getBookInfoExtralVo().getId());
				if(bookInfoExtral==null)
					bookInfoExtral=bookInfoDao.getBookInfoExtralByISBN(vo.getIsbn());
				if(bookInfoExtral==null)
					bookInfoExtral=new BookInfoExtral();
				bookInfoExtral.setIsbn(isbn);
				BeanUtils.copyProperties(vo.getBookInfoExtralVo(), bookInfoExtral,"id","isbn");
				bookInfoDao.addOrUpdateBookInfoExtral(bookInfoExtral);
			}
		}
		return bookInfo;
	}
	
	private void setBookInfoProperties(BookInfo bookInfo, BookInfoVO vo, String... exCludePropertyNames) {
		PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(bookInfo.getClass());
		if (descriptors != null && descriptors.length > 0) {
			for (PropertyDescriptor bookInfoPd : descriptors) {
				try {
					String propertyName = bookInfoPd.getName();
					boolean flag = false;
					if (exCludePropertyNames != null) {
						for (String exCludePropertyName : exCludePropertyNames) {
							if (exCludePropertyName.equalsIgnoreCase(propertyName)) {
								flag = true;
								break;
							}
						}
					}
					if (flag)
						continue;
					Method infoGetMethod = bookInfoPd.getReadMethod();
					if (infoGetMethod == null)
						continue;
					Method infoSetMethod = bookInfoPd.getWriteMethod();
					if (infoSetMethod == null)
						continue;
					PropertyDescriptor voPd = BeanUtils.getPropertyDescriptor(vo.getClass(), propertyName);
					if (voPd == null)
						continue;
					Method voGetMethod = voPd.getReadMethod();
					if (voGetMethod == null)
						continue;
					Method voSetMethod = voPd.getWriteMethod();
					if (voSetMethod == null)
						continue;
					Object oldValue = infoGetMethod.invoke(bookInfo);
					Object newValue = voGetMethod.invoke(vo);
					if ((oldValue == null && newValue != null) || (oldValue != null && newValue != null)) {
						infoSetMethod.invoke(bookInfo, newValue);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	private boolean chargeBookInfoExtralVoNullOrNotNull(BookInfoExtralVo vo){
		if(vo.getAuthorAffiliation()!=null&&vo.getAuthorAffiliation()!="")
			return true;
		if(vo.getAwards()!=null&&vo.getAwards()!="")
			return true;
		if(vo.getBindingIsbn()!=null&&vo.getBindingIsbn()!="")
			return true;
		if(vo.getBookType()!=null&&vo.getBookType()!="")
			return true;
		if(vo.getDdc()!=null&&vo.getDdc()!="")
			return true;
		if(vo.getEbookIsbn()!=null&&vo.getEbookIsbn()!="")
			return true;
		if(vo.getLcc()!=null&&vo.getLcc()!="")
			return true;
		if(vo.getLibraryCatalog()!=null&&vo.getLibraryCatalog()!="")
			return true;
		if(vo.getPreviousISBN()!=null&&vo.getPreviousISBN()!="")
			return true;
		if(vo.getSalesStatus()!=null&&vo.getSalesStatus()!="")
			return true;
		if(vo.getSetIsbn()!=null&&vo.getSetIsbn()!="")
			return true;
		if(vo.getUsp()!=null&&vo.getUsp()!="")
			return true;
		if(vo.getValidityOfCopyright()!=null&&vo.getValidityOfCopyright()!="")
			return true;
		if(vo.getProductId()!=null&&vo.getProductId()!="")
			return true;
		if(vo.getContents()!=null&&vo.getContents()!="")
			return true;
		if(vo.getReadership()!=null&&vo.getReadership()!="")
			return true;
		if(vo.getContents()!=null&&vo.getContents()!="")
			return true;
		return false;
	}

	@Override
	public void addOrUpdateBookInfoAsync(BookInfoDetailsVO vo) {
		BookInfo bookInfo = convertBookInfoDetailsVoToBookInfoReplacePropAnyWhere(vo);
		bookInfoDao.addOrUpdateBookInfo(bookInfo);
	}

	@Override
	public void rebuildIndex() {
		bookInfoFullTextSearchDao.rebuildIndex();
		
	}
	
	public void rebuildIndexBookInfoExtral() {
		bookInfoFullTextSearchDao.rebuildIndexBookInfoExtral();
	}

	private BookInfo convertBookInfoDetailsVoToBookInfoReplacePropAnyWhere(BookInfoDetailsVO vo) {
		if (StringUtils.isBlank(vo.getIsbn())) {
			throw new RuntimeException("ISBN号不能为空");
		}

		String isbn = vo.getIsbn();
		if (!IsbnConvertUtils.validate(isbn)) {
			throw new RuntimeException("ISBN号不正确");
		}

		if (StringUtils.isBlank(vo.getTitle())) {
			throw new RuntimeException("题名不能为空");
		}

		if (StringUtils.isBlank(vo.getPublisherFullName())) {
			throw new RuntimeException("出版社不能为空");
		}

		isbn = IsbnConvertUtils.convertTo13place(isbn);

		// 根据isbn号查找该书目
		BookInfo bookInfo = bookInfoDao.getBookInfoByIsbn(isbn);

		if (bookInfo == null) 
			bookInfo = new BookInfo();
		BeanUtils.copyProperties(vo, bookInfo, "isbn", "id", "subjectHeadings");
	    //设置时间
		if(vo.getPubYear()!=null){
			if(vo.getPubMonth()==null)
				vo.setPubMonth(0);
			if(vo.getPubDay()==null)
				vo.setPubDay(1);
			Date pubDate=DateHelper.getDate(vo.getPubYear(), vo.getPubMonth(), vo.getPubDay());
			bookInfo.setPubDate(pubDate);
			bookInfo.setPubYear(vo.getPubYear());
		}
		bookInfo.setIsbn(isbn);
		bookInfo.setLastUpdateDate(new Date());
		bookInfo.setPublisherFullName(vo.getPublisherFullName());
		bookInfo.setPublisherAlisName(vo.getPublisherAilsName());
		bookInfo.setCreateUserName(vo.getCreateUserName());
		
		if(vo.getBookInfoExtralVo()!=null){
			boolean saveOrNot=chargeBookInfoExtralVoNullOrNotNull(vo.getBookInfoExtralVo());
			if(saveOrNot){
				BookInfoExtral bookInfoExtral=bookInfoDao.getBookInfoExtralByISBN(isbn);
				if(bookInfoExtral==null&&vo.getBookInfoExtralVo().getId()!=null)
					bookInfoExtral=bookInfoDao.getBookInfoExtralById(vo.getBookInfoExtralVo().getId());
				if(bookInfoExtral==null)
					bookInfoExtral=bookInfoDao.getBookInfoExtralByISBN(vo.getIsbn());
				if(bookInfoExtral==null)
					bookInfoExtral=new BookInfoExtral();
				bookInfoExtral.setIsbn(isbn);
				BeanUtils.copyProperties(vo.getBookInfoExtralVo(), bookInfoExtral,"id","isbn");
				bookInfoDao.addOrUpdateBookInfoExtral(bookInfoExtral);
			}
		}
		return bookInfo;
	}

	@Override
	public PageViewVO<BookInfoDetailsVO> fullTextSearchByNormalSearchVO(BookInfoSearchNormalVO vo, int pageIndex,
			int pageSize, BookInfoPropertyLoadVo bookInfoPropertyLoadVo) {
		PageViewVO<BookInfo> pg = bookInfoFullTextSearchDao.getBookInfoByNormalSearchVO(vo, pageIndex, pageSize);
		return convertPageViewVoOfBookInfoToPageViewVoOfBookInfoDetailsVo(pg,bookInfoPropertyLoadVo);
	}
	
	private PageViewVO<BookInfoDetailsVO> convertPageViewVoOfBookInfoToPageViewVoOfBookInfoDetailsVo(PageViewVO<BookInfo> pg,BookInfoPropertyLoadVo
			 bookInfoPropertyLoadVo) {
		PageViewVO<BookInfoDetailsVO> pgVo = new PageViewVO<BookInfoDetailsVO>();
		BeanUtils.copyProperties(pg, pgVo, "datas", "additionalPara");
		List<BookInfoDetailsVO> vos = convertBookInfosToBookInfoDetailsVos(pg.getDatas(),bookInfoPropertyLoadVo);
		pgVo.setDatas(vos);
		return pgVo;
	}
	
	private List<BookInfoDetailsVO> convertBookInfosToBookInfoDetailsVos(List<BookInfo> books,BookInfoPropertyLoadVo
			 bookInfoPropertyLoadVo) {
		List<BookInfoDetailsVO> vos = new ArrayList<BookInfoDetailsVO>();
		List<String> ingnorePropertys=getBookInfoIngnoreProperty(bookInfoPropertyLoadVo);
		if(!ingnorePropertys.contains("publisherId"))
			ingnorePropertys.add("publisherId");
		if(!ingnorePropertys.contains("publisherAilsName"))
			ingnorePropertys.add("publisherAilsName");
		if(!ingnorePropertys.contains("ddcs"))
			ingnorePropertys.add("ddcs");
		if(!ingnorePropertys.contains("lccs"))
			ingnorePropertys.add("lccs");
		if(!ingnorePropertys.contains("subjectVos"))
			ingnorePropertys.add("subjectVos");
		if(!ingnorePropertys.contains("isDel"))
			ingnorePropertys.add("isDel");
		if(!ingnorePropertys.contains("bookInfoExtral"))
			ingnorePropertys.add("bookInfoExtral");
		String[] ingnorePropertysArray=new String[ingnorePropertys.size()];
		ingnorePropertysArray=ingnorePropertys.toArray(ingnorePropertysArray);
		
		String[] bookInfoExtralVoIngnorePropertys=getBookInfoExtralIngnoreProperty(bookInfoPropertyLoadVo);
		
		if (books != null && books.size() > 0) {
			for (BookInfo book : books) {
				BookInfoDetailsVO vo = convertToBookInfoDetailsVO(book,ingnorePropertysArray,bookInfoExtralVoIngnorePropertys);
				if (vo != null) {
					vos.add(vo);
				}
			}
		}
		return vos;
	}
	
	private BookInfoDetailsVO convertToBookInfoDetailsVO(BookInfo book,String[] ingnoreBookInfoPropertys,String[] ingnoreBookInfoExtralPropertys
			 ) {
		if (book == null)
			return null;
		
		//TODO:重新写方法
		BookInfoDetailsVO vo = new BookInfoDetailsVO();		
		copyProperties(book, vo, null,ingnoreBookInfoPropertys);
		if(book.getPubYear()!=null)
			vo.setPubYear(book.getPubYear());
		if(book.getPubDate()!=null){
			int year=DateHelper.getYearFromDate(book.getPubDate());
			int month=DateHelper.getMonthFromDate(book.getPubDate());
			int day=DateHelper.getDayFromDate(book.getPubDate());
			if(vo.getPubYear()==null)
				vo.setPubYear(year);
			vo.setPubMonth(month);
			vo.setPubDay(day);
			if(month==1&&day==1)
				vo.setPubDateStr(DateHelper.toStirng(book.getPubDate(), "yyyy"));
			else
				vo.setPubDateStr(DateHelper.toStirng(book.getPubDate(), "yyyy-MM-dd"));
		}
		if(ingnoreBookInfoExtralPropertys==null||(ingnoreBookInfoExtralPropertys!=null&&ingnoreBookInfoExtralPropertys.length<18)){
			BookInfoExtral bookInfoExtral=bookInfoFullTextSearchDao.getFullTextSearchBookInfoExtralByIsbn(book.getIsbn());
			if(bookInfoExtral!=null){
				BookInfoExtralVo bookInfoExtralVo=new BookInfoExtralVo();
				copyProperties(bookInfoExtral,bookInfoExtralVo,null,ingnoreBookInfoExtralPropertys);
				vo.setBookInfoExtralVo(bookInfoExtralVo);
			}
		}
		return vo;
	}

	
	private List<String> getBookInfoIngnoreProperty(BookInfoPropertyLoadVo vo){
		if(vo==null)
			return null;
		List<String> excludeVo=new ArrayList<String>();
		java.lang.reflect.Field[] fields=vo.getClass().getDeclaredFields();
		for (java.lang.reflect.Field field : fields) {
			try {
				field.setAccessible(true);
				Object obj=field.get(vo);
				boolean load=Boolean.parseBoolean(obj.toString());
				if(field.getName().startsWith("extral_")&&load)
					vo.setBookInfoExtralVo(true);
				if(!load)
					excludeVo.add(field.getName());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return excludeVo;
	}
	
	private String[] getBookInfoExtralIngnoreProperty(BookInfoPropertyLoadVo vo){
		if(vo==null)
			return null;
		List<String> excludeVo=new ArrayList<String>();
		Class<? extends BookInfoPropertyLoadVo> voClass=vo.getClass();
		java.lang.reflect.Field[] fields=voClass.getDeclaredFields();
		for (java.lang.reflect.Field field : fields) {
			try {
				if(field.getName().startsWith("extral_")){
					field.setAccessible(true);
					Object obj=field.get(vo);
					if(!Boolean.parseBoolean(obj.toString()))
						excludeVo.add(field.getName().replace("extral_", ""));
				}
			} catch (IllegalArgumentException e) {
				
			} catch (IllegalAccessException e) {
				
			}
		}
		String[] ingnorePropertysArray=new String[excludeVo.size()];
		ingnorePropertysArray=excludeVo.toArray(ingnorePropertysArray);
		return ingnorePropertysArray;
	}

	private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
			throws BeansException {

		Assert.notNull(source, "Source must not be null");
		Assert.notNull(target, "Target must not be null");

		Class<?> actualEditable = target.getClass();
		if (editable != null) {
			if (!editable.isInstance(target)) {
				throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
						"] not assignable to Editable class [" + editable.getName() + "]");
			}
			actualEditable = editable;
		}
		PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
		List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;

		for (PropertyDescriptor targetPd : targetPds) {
			Method writeMethod = targetPd.getWriteMethod();
			if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
				PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
				if (sourcePd != null) {
					Method readMethod = sourcePd.getReadMethod();
					if (readMethod != null &&
							ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
						try {
							if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
								readMethod.setAccessible(true);
							}
							Object value = readMethod.invoke(source);
							if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
								writeMethod.setAccessible(true);
							}
							writeMethod.invoke(target, value);
						}
						catch (Throwable ex) {
							throw new FatalBeanException(
									"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
						}
					}
				}
			}
		}
	}

	
	public void addOrUpdatePublisher(String publisher,String alis){
		if(publisher==null||publisher=="")
			return;
		BookInfoPublisher bookInfoPublisher=bookInfoPublisherDao.getByName(publisher);
		if(bookInfoPublisher==null)
			bookInfoPublisher=new BookInfoPublisher();
		bookInfoPublisher.setName(publisher);
		bookInfoPublisher.setAils(alis);
		bookInfoPublisherDao.addOrUpdateBookinfoPublisher(bookInfoPublisher);
	}
	
	public void addOrUpdateCategory1AndCategory(String category1,String category2){
		if(category1==null||category1=="")
			return;
		BookInfoCategory bookInfoCategory=bookInfoCategoryDao.getTopCategpryByName(category1);
		if(bookInfoCategory==null)
			bookInfoCategory=new BookInfoCategory();
		bookInfoCategory.setCategory(category1);
		bookInfoCategory.setLevel(1);
		bookInfoCategory.setParentId(null);
		bookInfoCategoryDao.addOrUpdateBookInfoCategory(bookInfoCategory);
		if(category2==null||category2=="")
			return ;
		BookInfoCategory bookInfoCategory2=bookInfoCategoryDao.getChildCategoryByNameAndParent(category2, bookInfoCategory.getId());
		if(bookInfoCategory2==null)
			bookInfoCategory2=new BookInfoCategory();
		bookInfoCategory2.setCategory(category2);
		bookInfoCategory2.setLevel(2);
		bookInfoCategory2.setParentId(bookInfoCategory.getId());
		bookInfoCategoryDao.addOrUpdateBookInfoCategory(bookInfoCategory2);
	}



	@Override
	public List<BookInfoCategoryVos> getAllBookInfoCategoryVos() {
		List<BookInfoCategory> topBookCategory=bookInfoCategoryDao.getAllTopCategory();
		List<BookInfoCategoryVos> vos=new ArrayList<BookInfoCategoryVos>();
		if(topBookCategory==null||topBookCategory.size()==0)
			return null;
		for (BookInfoCategory bookInfoCategory : topBookCategory) {
			BookInfoCategoryVos vo=new BookInfoCategoryVos();
			vo.setId(bookInfoCategory.getId());
			vo.setLevel(1);
			vo.setName(bookInfoCategory.getCategory());

			List<BookInfoCategory> childrenBookCategorys=bookInfoCategoryDao.getChildrenCategoryByParentId(bookInfoCategory.getId());
			for (BookInfoCategory bookInfoCategory2 : childrenBookCategorys) {
				BookInfoCategoryVO childVo=new BookInfoCategoryVO();
				childVo.setId(bookInfoCategory2.getId());
				childVo.setLevel(bookInfoCategory2.getLevel());
				childVo.setName(bookInfoCategory2.getCategory());
				vo.getChildren().add(childVo);
			}
			
			vos.add(vo);
		}
		return vos;
	}



	@Override
	public List<BookInfoPublisherVo> getAllBookInfoPublisherVos() {
		List<BookInfoPublisherVo> vos=new ArrayList<BookInfoPublisherVo>();
		List<BookInfoPublisher> publishers=bookInfoPublisherDao.getAllPublisher();
		if(publishers==null||publishers.size()==0)
			return vos;
		for (BookInfoPublisher bookInfoPublisher : publishers) {
			BookInfoPublisherVo vo=new BookInfoPublisherVo();
			vo.setId(bookInfoPublisher.getId());
			vo.setAils(bookInfoPublisher.getAils());
			vo.setName(bookInfoPublisher.getName());
			vos.add(vo);
		}
		return vos;
	}
	
	public void updateBookInfoIsBlackBookByIsbns(String[] isbns,boolean isBlackBook){
		if(isbns==null||isbns.length==0)
			return;
		List<BookInfo> bookInfos=bookInfoFullTextSearchDao.getFullTextBookInfoByIsbns(isbns);
		if(bookInfos==null||bookInfos.size()==0)
			return;
		for (BookInfo bookInfo : bookInfos) {
			bookInfo.setIsBlackBook(isBlackBook);
			bookInfoDao.addOrUpdateBookInfo(bookInfo);
		}
	}



	@Override
	public String updateBookInfoPriceAndCurrency(BookInfoUpdateVo[] updateVos) {
		if(updateVos==null||updateVos.length==0)
			return "没有可提交的数据";
		String errorMsg="";
		for (int i = 0; i < updateVos.length; i++) {
			BookInfoUpdateVo currentVo=updateVos[i];
			try {	
				if(StringUtils.isBlank(currentVo.getIsbn()))
					continue;
				BookInfo bookInfo=bookInfoFullTextSearchDao.getUniqueBookByIsbn(currentVo.getIsbn());
				if(bookInfo!=null){
					if(StringUtils.isNotBlank(currentVo.getCurrency()))
						bookInfo.setCurrency(currentVo.getCurrency());
					if(currentVo.getPrice()!=null&&currentVo.getPrice()>0)
						bookInfo.setPrice(currentVo.getPrice().floatValue());
					bookInfoDao.addOrUpdateBookInfo(bookInfo);
				}
				if(StringUtils.isNotBlank(currentVo.getSaleStatus())){
					BookInfoExtral bookInfoExtral=bookInfoFullTextSearchDao.getFullTextSearchBookInfoExtralByIsbn(currentVo.getIsbn());
					if(bookInfoExtral!=null){
						bookInfoExtral.setSalesStatus(currentVo.getSaleStatus());
						bookInfoDao.addOrUpdateBookInfoExtral(bookInfoExtral);
					}
				}
			} catch (Exception e) {
				errorMsg+="数据："+currentVo.getIsbn()+e.getMessage()+";";
			}
		}
		if(errorMsg=="")
			errorMsg="批量修改成功";
		return errorMsg;
	}



	@Override
	public void deleteBookInfoByIsbns(String[] isbns) {
		if(isbns==null||isbns.length==0)
			return;
		for (int i = 0; i < isbns.length; i++) {
			BookInfo bookInfo=bookInfoFullTextSearchDao.getUniqueBookByIsbn(isbns[i]);
			if(bookInfo!=null)
				bookInfoDao.deleteBookInfo(bookInfo);
			BookInfoExtral bookInfoExtral=bookInfoFullTextSearchDao.getFullTextSearchBookInfoExtralByIsbn(isbns[i]);
			if(bookInfoExtral!=null)
				bookInfoDao.deleteBookInfoExtral(bookInfoExtral);
		}
	}
	
	public  List<BookInfoStatisticsTJVo> getBookInfoStatisticsVosByBookInfoSearchNormalVO(BookInfoSearchNormalVO bookInfoSearchNormalVO) {
		 List<BookInfoStatisticsTJVo> vos=bookInfoFullTextSearchDao.groupingByQuery(bookInfoSearchNormalVO);
		return vos;
	}
}
